package com.module.record.controller;

import com.alibaba.fastjson.JSONObject;
import com.module.common.annotation.Log;
import com.module.common.core.controller.BaseController;
import com.module.common.core.domain.AjaxResult;
import com.module.common.core.page.TableDataInfo;
import com.module.common.enums.BusinessType;
import com.module.common.utils.DateUtils;
import com.module.common.utils.ShiroUtils;
import com.module.common.utils.poi.ExcelUtil;
import com.module.record.domain.RecordControl;
import com.module.record.service.IRecordControlService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备控制Controller
 */
@Controller
@RequestMapping("/record/control")
public class RecordControlController extends BaseController {

    private String prefix = "record/control";

    @Autowired
    private IRecordControlService recordControlService;

    @RequiresPermissions("record:control:view")
    @GetMapping()
    public String control(ModelMap mmap)
    {
        mmap.put("startDate", DateUtils.getMonth() + "-01");
        mmap.put("endDate", DateUtils.getDate());
        return prefix + "/control";
    }

    /**
     * 查询采集设备列表
     */
    @RequiresPermissions("record:control:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordControl recordControl)
    {
        startPage();
        List<RecordControl> list = recordControlService.selectRecordControlList(recordControl);
        return getDataTable(list);
    }

    /**
     * 控制记录
     * @param recordControl
     * @return
     */
    @Log(title = "控制记录", businessType = BusinessType.EXPORT)
    @RequiresPermissions("record:control:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecordControl recordControl)
    {
        List<RecordControl> list = recordControlService.selectRecordControlList(recordControl);
        ExcelUtil<RecordControl> util = new ExcelUtil<RecordControl>(RecordControl.class);
        return util.exportExcel(list, "控制记录");
    }

    /**
     * 获取服务器地址
     * @param deviceIds
     * @return
     */
    @RequiresPermissions("basic:device:getinfo")
    @Log(title = "获取设备信息", businessType = BusinessType.INSERT)
    @PostMapping("/getinfo")
    @ResponseBody
    public AjaxResult getinfo(String deviceIds) {
        String controlOrder = "NAT@AT+BLDEVINFO";
        String message = recordControlService.saveRecordControl(deviceIds, "getinfo", "", controlOrder, ShiroUtils.getLoginName(), "获取设备信息");
        return AjaxResult.success(message);
    }

    /**
     * 获取服务器地址
     * @param deviceIds
     * @return
     */
    @RequiresPermissions("basic:device:getserver")
    @Log(title = "获取服务器地址", businessType = BusinessType.INSERT)
    @PostMapping("/getserver")
    @ResponseBody
    public AjaxResult getserver(String deviceIds, String socketType) {
        String controlOrder = "NAT@AT+SOCK1" + socketType;
        String message = recordControlService.saveRecordControl(deviceIds, "getserver", socketType, controlOrder, ShiroUtils.getLoginName(), "获取服务器地址");
        return AjaxResult.success(message);
    }

    /**
     * 设置服务器地址
     */
    @GetMapping("/setserver/{deviceIds}")
    public String setserver(@PathVariable("deviceIds") String deviceIds, ModelMap mmap)
    {
        mmap.put("deviceIds", deviceIds);
        return prefix + "/set_server";
    }

    /**
     * 设置服务器地址
     */
    @RequiresPermissions("basic:device:setserver")
    @Log(title = "修改服务器地址", businessType = BusinessType.INSERT)
    @PostMapping("/setserver")
    @ResponseBody
    public AjaxResult setserver(String deviceIds, String socketType, String server, String port)
    {
        JSONObject controlParam = new JSONObject();
        controlParam.put("serverIp", server);
        controlParam.put("serverPort", port);
        controlParam.put("socketType", socketType);
        String controlOrder = "NAT@AT+SOCK1" + socketType + "=TCPC," + server + "," + port;
        String message = recordControlService.saveRecordControl(deviceIds, "setserver", controlParam.toJSONString(), controlOrder, ShiroUtils.getLoginName(), "修改服务器地址");
        return AjaxResult.success(message);
    }

    /**
     * 关闭通道
     * @param deviceIds
     * @return
     */
    @RequiresPermissions("basic:device:offserver")
    @Log(title = "关闭通道", businessType = BusinessType.INSERT)
    @PostMapping("/offserver")
    @ResponseBody
    public AjaxResult offserver(String deviceIds, String socketType) {
        String controlOrder = "NAT@AT+SOCKEN1" + socketType + "=OFF";
        String message = recordControlService.saveRecordControl(deviceIds, "offserver", socketType, controlOrder, ShiroUtils.getLoginName(), "通道关闭");
        return AjaxResult.success(message);
    }

    /**
     * 设备升级
     */
    @GetMapping("/upgrade/{deviceIds}")
    public String upgrade(@PathVariable("deviceIds") String deviceIds, ModelMap mmap)
    {
        mmap.put("deviceIds", deviceIds);
        return prefix + "/upgrade";
    }

    /**
     * 设备升级
     */
    @RequiresPermissions("basic:device:upgrade")
    @Log(title = "设备程序升级", businessType = BusinessType.INSERT)
    @PostMapping("/upgrade")
    @ResponseBody
    public AjaxResult upgrade(String deviceIds, String url)
    {
        String controlOrder = "NAT@AT+BLUPDATEALL=" + url;
        String message = recordControlService.saveRecordControl(deviceIds, "upgrade", url, controlOrder, ShiroUtils.getLoginName(), "设备程序升级");
        return AjaxResult.success(message);
    }
}
